package com.zhouyu.mysql.core;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.LineSeparator;
import cn.hutool.json.JSONUtil;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * 大都督周瑜（我的私人微信: dadudu6789）
 */
public class FileSort {

    public static File sort(File tmpDir) throws IOException {
        File[] files = tmpDir.listFiles();

        if (files.length == 1) {
            return files[0];
        } else {
            megeTwoFiles(files[0], files[1], tmpDir);
            return sort(tmpDir);
        }

    }


    private static void megeTwoFiles(File file1, File file2, File tmpDir) throws IOException {


        BufferedReader reader1 = FileUtil.getUtf8Reader(file1);
        BufferedReader reader2 = FileUtil.getUtf8Reader(file2);
        BufferedWriter writer = FileUtil.getWriter(FileUtil.createTempFile(tmpDir), StandardCharsets.UTF_8, true);

        String s1 = reader1.readLine();
        String s2 = reader2.readLine();
        while (s1 != null && s2 != null) {
            BufEntry bufEntry1 = JSONUtil.toBean(s1, BufEntry.class);
            BufEntry bufEntry2 = JSONUtil.toBean(s2, BufEntry.class);

            if (bufEntry1.compareTo(bufEntry2) <= 0) {
                writer.write(s1);
                writer.newLine();
                s1 = reader1.readLine();
            } else {
                writer.write(s2);
                writer.newLine();
                s2 = reader2.readLine();
            }
        }
        while (s1 != null) {
            writer.write(s1);
            writer.newLine();
            s1 = reader1.readLine();
        }
        while (s2 != null) {
            writer.write(s2);
            writer.newLine();
            s2 = reader2.readLine();
        }
        reader1.close();
        reader2.close();
        writer.close();

        file1.delete();
        file2.delete();
    }


    public static void main(String[] args) {

        File tmpDir = FileUtil.file("tmp");

        List<BufEntry> buf1 = new ArrayList<>();

        BufEntry entry1 = new BufEntry();
        entry1.setId(1);
        entry1.getFields().add("a");
        entry1.getFields().add("j");

        BufEntry entry2 = new BufEntry();
        entry2.setId(4);
        entry2.getFields().add("b");
        entry2.getFields().add("i");

        buf1.add(entry1);
        buf1.add(entry2);

        File tempFile1 = FileUtil.createTempFile(tmpDir);
        for (BufEntry entry : buf1) {
            FileUtil.appendUtf8String(JSONUtil.toJsonStr(entry), tempFile1);
            FileUtil.appendUtf8String(LineSeparator.MAC.getValue(), tempFile1);
        }


        List<BufEntry> buf2 = new ArrayList<>();

        BufEntry entry3 = new BufEntry();
        entry3.setId(2);
        entry3.getFields().add("b");
        entry3.getFields().add("f");

        BufEntry entry4 = new BufEntry();
        entry4.setId(7);
        entry4.getFields().add("f");
        entry4.getFields().add("a");

        buf2.add(entry3);
        buf2.add(entry4);

        File tempFile2 = FileUtil.createTempFile(tmpDir);
        for (BufEntry entry : buf2) {
            FileUtil.appendUtf8String(JSONUtil.toJsonStr(entry), tempFile2);
            FileUtil.appendUtf8String(LineSeparator.MAC.getValue(), tempFile2);
        }



        List<BufEntry> buf3 = new ArrayList<>();

        BufEntry entry5 = new BufEntry();
        entry5.setId(3);
        entry5.getFields().add("b");
        entry5.getFields().add("h");

        BufEntry entry6 = new BufEntry();
        entry6.setId(8);
        entry6.getFields().add("g");
        entry6.getFields().add("h");

        buf3.add(entry5);
        buf3.add(entry6);

        File tempFile3 = FileUtil.createTempFile(tmpDir);
        for (BufEntry entry : buf3) {
            FileUtil.appendUtf8String(JSONUtil.toJsonStr(entry), tempFile3);
            FileUtil.appendUtf8String(LineSeparator.MAC.getValue(), tempFile3);
        }

        try {
            File result = sort(tmpDir);
            System.out.println(result.getPath());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
